package com.zjl.paike2.controller;

import com.zjl.paike2.dto.TeacherDTO;
import com.zjl.paike2.entity.TeacherInfo;
import com.zjl.paike2.service.TeacherInfoService;
import com.zjl.paike2.vo.PageResult;
import com.zjl.paike2.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 教师管理Controller
 */
@RestController
@RequestMapping("/teachers")
public class TeacherController {
    
    @Autowired
    private TeacherInfoService teacherInfoService;
    
    /**
     * 查询教师列表
     */
    @GetMapping
    public Result<PageResult<TeacherInfo>> getTeacherList(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        PageResult<TeacherInfo> pageResult = teacherInfoService.getTeacherList(pageNum, pageSize);
        return Result.success(pageResult);
    }
    
    /**
     * 根据编号查询教师
     */
    @GetMapping("/{teacherCode}")
    public Result<TeacherInfo> getTeacherByCode(@PathVariable String teacherCode) {
        TeacherInfo teacherInfo = teacherInfoService.getTeacherByCode(teacherCode);
        if (teacherInfo != null) {
            return Result.success(teacherInfo);
        }
        return Result.error("教师不存在");
    }
    
    /**
     * 添加教师
     */
    @PostMapping
    public Result<Void> addTeacher(@RequestBody TeacherDTO teacherDTO) {
        // 不再输出完整的教师信息（包含密码）到日志
        System.out.println("接收到添加教师请求，教师编号: " + teacherDTO.getTeacher_code() + ", 姓名: " + teacherDTO.getTeacher_name());
        
        // 检查请求体中的必要字段
        if (teacherDTO == null) {
            return Result.error("请求体不能为空");
        }
        
        if (teacherDTO.getTeacher_code() == null || teacherDTO.getTeacher_name() == null) {
            return Result.error("教师编号和姓名不能为空");
        }
        
        // 将DTO转换为实体
        TeacherInfo teacherInfo = new TeacherInfo();
        teacherInfo.setTeacherCode(teacherDTO.getTeacher_code())
                  .setTeacherName(teacherDTO.getTeacher_name())
                  .setPd(teacherDTO.getPd())  // 密码会在实体类中进行处理
                  .setTeacherTitle(teacherDTO.getTeacher_title())
                  .setSex(teacherDTO.getSex())
                  .setCollegeNo(teacherDTO.getCollege_no());
        
        boolean success = teacherInfoService.addTeacher(teacherInfo);
        if (success) {
            return Result.success();
        }
        return Result.error("教师编号已存在");
    }
    
    /**
     * 修改教师
     */
    @PutMapping("/{teacherCode}")
    public Result<Void> updateTeacher(@PathVariable String teacherCode, @RequestBody TeacherDTO teacherDTO) {
        // 将DTO转换为实体
        TeacherInfo teacherInfo = new TeacherInfo();
        if (teacherDTO.getTeacher_name() != null) {
            teacherInfo.setTeacherName(teacherDTO.getTeacher_name());
        }
        if (teacherDTO.getTeacher_title() != null) {
            teacherInfo.setTeacherTitle(teacherDTO.getTeacher_title());
        }
        if (teacherDTO.getSex() != null) {
            teacherInfo.setSex(teacherDTO.getSex());
        }
        if (teacherDTO.getCollege_no() != null) {
            teacherInfo.setCollegeNo(teacherDTO.getCollege_no());
        }
        // 密码不通过普通更新进行修改，应该有专门的修改密码接口
        
        boolean success = teacherInfoService.updateTeacher(teacherCode, teacherInfo);
        if (success) {
            return Result.success();
        }
        return Result.error("教师不存在");
    }
    
    /**
     * 删除教师
     */
    @DeleteMapping("/{teacherCode}")
    public Result<Void> deleteTeacher(@PathVariable String teacherCode) {
        boolean success = teacherInfoService.deleteTeacher(teacherCode);
        if (success) {
            return Result.success();
        }
        return Result.error("教师不存在");
    }
} 